package com.zbkc.main;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.zbkc.main.base.BaseTitleActivity;
import com.zbkc.util.MyApplication;

public class MoreActivity extends BaseTitleActivity implements
		AdapterView.OnItemClickListener, View.OnClickListener {

	private long firstBackT = 0L;
	private boolean isFirstBack = false;
	private ListView listView;
	private List<String> mList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		bindView();
	}

	private void bindView() {
		setTitle("更多");
		setBtn1Visiable(false);
		setBtn2Visiable(false);
		listView = ((ListView) findViewById(R.id.listView));
		listView.setOnItemClickListener(this);
		mList = new ArrayList();
		mList.add("本地文件管理");
		MyAdapter localMyAdapter = new MyAdapter(this, 0, this.mList);
		this.listView.setAdapter(localMyAdapter);
		findViewById(R.id.btn_exit).setOnClickListener(this);
	}

	public void onBackPressed() {
		if (!this.isFirstBack) {
			this.firstBackT = System.currentTimeMillis();
			Toast.makeText(this, "再按一次退出程序！", 0).show();
			this.isFirstBack = true;
			new Thread(new Runnable() {
				public void run() {
					try {
						Thread.sleep(5000L);
						MoreActivity.this.isFirstBack = false;
						return;
					} catch (InterruptedException localInterruptedException) {
					}
				}
			}).start();
			return;
		}
		MyApplication.getInstance().finishAllActivity();
		System.gc();
		System.exit(0);
		super.onBackPressed();
	}

	public void onClick(View paramView) {
		switch (paramView.getId()) {
		case R.id.btn_exit:
			MyApplication.getInstance().finishAllActivity();
			Intent localIntent = new Intent();
			localIntent.setClass(this, LoginActivity.class);
			startActivity(localIntent);
			break;
		}
	}

	public void onItemClick(AdapterView<?> paramAdapterView, View paramView,
			int paramInt, long paramLong) {
		 Intent localIntent = new Intent();
		 localIntent.setClass(this, FileManagerActivity.class);
		 startActivity(localIntent);
	}

	public boolean onKeyDown(int paramInt, KeyEvent paramKeyEvent) {
		if ((paramInt == 4) && (paramKeyEvent.getRepeatCount() == 0)) {
			onBackPressed();
			return true;
		}
		return super.onKeyDown(paramInt, paramKeyEvent);
	}

	class MyAdapter extends ArrayAdapter<String> {

		public MyAdapter(Context context, int textViewResourceId,
				List<String> objects) {
			super(context, textViewResourceId, objects);
		}

		public View getView(int paramInt, View paramView,
				ViewGroup paramViewGroup) {
			ViewHolder viewHolder;
			if (paramView == null) {
				viewHolder = new ViewHolder();
				paramView = View.inflate(getContext(),
						R.layout.listview_more_item, null);
				viewHolder.tvtitle = ((TextView) paramView
						.findViewById(R.id.tv_title));
				paramView.setTag(viewHolder);
			} else {
				viewHolder = ((ViewHolder) paramView.getTag());
			}
			viewHolder.tvtitle.setText((mList.get(paramInt)).toString());

			return paramView;
		}

		class ViewHolder {
			TextView tvtitle;
		}
	}

}
